<?php
namespace App\Controllers;

use App\Models\User;

class UserController {
    private $userModel;
    
    public function __construct() {
        $this->userModel = new User();
    }
    
    public function index($uri) {
        $users = $this->userModel->getAll();
        http_response_code(200);
        header('Content-Type: application/json');
        echo json_encode(['success' => true, 'data' => $users]);
    }
    
    public function show($uri) {
        // 从 URI 中提取 ID (简单处理)
        $parts = explode('/', trim($uri, '/'));
        $id = end($parts);

        $user = $this->userModel->getById($id);
        
        if ($user) {
            http_response_code(200);
            header('Content-Type: application/json');
            echo json_encode(['success' => true, 'data' => $user]);
        } else {
            http_response_code(404);
            header('Content-Type: application/json');
            echo json_encode(['success' => false, 'error' => 'User not found']);
        }
    }
    
    public function create($uri) {
        $data = json_decode(file_get_contents('php://input'), true);
        
        if (empty($data['name'])) {
            http_response_code(400);
            header('Content-Type: application/json');
            echo json_encode(['success' => false, 'error' => 'Name and email are required']);
            return;
        }
        
        $result = $this->userModel->create($data);
        
        if ($result) {
            http_response_code(201);
            header('Content-Type: application/json');
            echo json_encode(['success' => true, 'data' => $result]);
        } else {
            http_response_code(500);
            header('Content-Type: application/json');
            echo json_encode(['success' => false, 'error' => 'Failed to create user']);
        }
    }

    // 更新用户
    public function update($uri) {
        $parts = explode('/', trim($uri, '/'));
        $sid = end($parts);

        $data = json_decode(file_get_contents('php://input'), true);

        if (empty($data['name'])) {
            http_response_code(400);
            header('Content-Type: application/json');
            echo json_encode(['success' => false, 'error' => 'Name is required']);
            return;
        }

        $result = $this->userModel->update($sid, $data);

        if ($result) {
            http_response_code(200);
            header('Content-Type: application/json');
            echo json_encode(['success' => true, 'message' => 'User updated successfully']);
        } else {
            http_response_code(500);
            header('Content-Type: application/json');
            echo json_encode(['success' => false, 'error' => 'Failed to update user']);
        }
    }

    // 删除用户
    public function delete($uri) {
        $parts = explode('/', trim($uri, '/'));
        $sid = end($parts);

        $result = $this->userModel->delete($sid);

        if ($result) {
            http_response_code(200);
            header('Content-Type: application/json');
            echo json_encode(['success' => true, 'message' => 'User deleted successfully']);
        } else {
            http_response_code(500);
            header('Content-Type: application/json');
            echo json_encode(['success' => false, 'error' => 'Failed to delete user']);
        }
    }
    
    public function search($uri){
        // 从 URI 中提取查询的sid和name
        $sid = $_GET['sid'] ?? '';
        $name = $_GET['name'] ?? '';
        $major = $_GET['major'] ?? '';
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
        $pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] : 10;

        // 确保页码和每页大小在合理范围
        $page = max(1, $page);
        $pageSize = max(5, min(100, $pageSize));

        // 调用模型进行搜索
        $result = $this->userModel->search($sid, $name, $major, $page, $pageSize);

        if ($result !== false) {
            http_response_code(200);
            header('Content-Type: application/json');
            echo json_encode([
                'success' => true, 
                'data' => $result['data'],
                'pagination' => [
                    'total' => $result['total'],
                    'page' => $result['page'],
                    'pageSize' => $result['pageSize'],
                    'totalPages' => $result['totalPages']
                ]
            ]);
        } else {
            http_response_code(500);
            header('Content-Type: application/json');
            echo json_encode(['success' => false, 'error' => 'Failed to search users']);
        }
    }
}